package com.zhongxiangspace.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhongxiangspace.entity.ArticleType;
import com.zhongxiangspace.entity.Articles;
import com.zhongxiangspace.entity.FreemarkerHtmlConfiguration;
import com.zhongxiangspace.entity.QueryAnalyzer;
import com.zhongxiangspace.result.ExceptionResult;
import com.zhongxiangspace.result.NormalResult;
import com.zhongxiangspace.result.Result;
import com.zhongxiangspace.service.ArticleService;
import com.zhongxiangspace.service.OfficeBuildingService;
import com.zhongxiangspace.tools.FreeMarkerViewUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新闻静态页面
 */
@Controller
public class NewsDetailMobileHtmlCreateController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private OfficeBuildingService officeBuildingService;

    public boolean ajxaCreatHtmlToolsNews(Integer id) {
        try {
            FreemarkerHtmlConfiguration freemarkerHtmlConfiguration = new FreemarkerHtmlConfiguration();
            freemarkerHtmlConfiguration.setTemplateName("mobile_news.ftl");
            freemarkerHtmlConfiguration.setHtmlPath("/mnt/html/wap/details/news"+id+".html");
            freemarkerHtmlConfiguration.setTemplatePath("html/mobile");
            Map<String, Object> map = new HashMap<>();
            Articles articles = this.articleService.queryOne(id);
            map.put("articles", articles);//news 内容
            ArticleType articleType = this.articleService.queryTypeOne(articles.getArticle_type_id());
            map.put("articleType", articleType);//文章类型
            //热门楼盘
            QueryAnalyzer buildingQueryAnalyzer = new QueryAnalyzer();
            buildingQueryAnalyzer.setShowCode(1);
            buildingQueryAnalyzer.setHotCode(1);
            List<Map<String, Object>> officeBuildingsList = this.officeBuildingService.queryOfficeBuilding(buildingQueryAnalyzer);
            map.put("officeBuildings", officeBuildingsList);
            //热门文章
            QueryAnalyzer newsQueryAnalyzer = new QueryAnalyzer();
            newsQueryAnalyzer.setShowCode(1);
            newsQueryAnalyzer.setHotCode(1);
            List<Map<String, Object>> articleHotList = this.articleService.articlesAll(newsQueryAnalyzer);
            map.put("articleHotList", articleHotList);
            //最新文章
            QueryAnalyzer newQueryAnalyzer = new QueryAnalyzer();
            newQueryAnalyzer.setShowCode(1);
            PageHelper.startPage(1, 10, "articles.modify_time  DESC");
            Page<Map<String, Object>> articlenewList = (Page<Map<String, Object>>) this.articleService.articlesAll(newQueryAnalyzer);
            map.put("articlenewList", articlenewList.getResult());
            freemarkerHtmlConfiguration.setData(map);
            boolean flag= FreeMarkerViewUtil.createHtml(freemarkerHtmlConfiguration);
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @ResponseBody
    @PostMapping("/htmlCreate/html/mobileNewsdetails.do")
    public Result ajxaCreateNews(HttpServletRequest request, String pageType) {
        try {
            QueryAnalyzer queryAnalyzer = new QueryAnalyzer();
            List<Map<String, Object>> stationBranchList = this.articleService.articlesAll(queryAnalyzer);
            for (int i = 0; i < stationBranchList.size(); i++) {
                this.ajxaCreatHtmlToolsNews((Integer) stationBranchList.get(i).get("article_id"));
            }
            return new NormalResult(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExceptionResult(e);
        }
    }


}
